<?php

class Lesson extends Zend_Db_Table
{
    protected $_name = 'lesson';
    
    public function checkExist($user_id,$authority_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('
                SELECT count(*) as all_records 
                FROM rights 
                WHERE user_id="'.(int)$user_id.'"
                AND authority_id="'.(int)$authority_id.'"');
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0]['all_records'];
    }
    
    public function ClearLinks($user_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('DELETE FROM rights 
                                   WHERE user_id="'.$user_id.'"');
    }
    
    public function AddRights($user_id,$authority_id)
    {
        $data = array(
                    'authority_id' => $authority_id,
                    'user_id' => $user_id);
        $this->insert($data);
    }
    
    public function CanDo($user_id,$controller,$action)
    {
        $select = $this->getAdapter();
        $q = 'SELECT count(authority.id) as all_records 
              FROM authority 
              INNER JOIN groupauthority on groupauthority.authority_id = authority.id
              INNER JOIN user on user.authgroup_id = groupauthority.authgroup_id
              WHERE authority.controller="'.$controller.'"
              AND authority.action="'.$action.'"
              AND user.id="'.$user_id.'"';
        //echo $q.'<br>'; exit();
        $prepare = $select->query($q);
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0]['all_records']>0;
    }
    
    public function CanSeeProject($user_id,$project_id)
    {
        $select = $this->getAdapter();
        $query = 'SELECT count(*) as all_records 
                FROM userprojectlinks
                WHERE userprojectlinks.user_id = "'.(int)$user_id.'" AND
                userprojectlinks.project_id = "'.(int)$project_id.'"';
        $prepare = $select->query($query);
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0]['all_records']>0;
    }
    
    public function getLessonsText($date,$user_id,$authgroup_id) {
        
        $return = '';
        if($authgroup_id==2) $add = ' AND teacher_id="'.$user_id.'"';
        else $add = '';
        
        $select = $this->getAdapter();
        $query = 'SELECT lesson.*, studygroup.name 
                  FROM lesson
                  LEFT JOIN studygroup on studygroup.id = lesson.studygroup_id 
                  WHERE lesson.deleted!=1
                  AND lesson.start_date="'.$date.'"'.$add;
        $prepare = $select->query($query);
        $fetchAll = $prepare->fetchAll(Zend_Db::FETCH_OBJ);
        
        foreach($fetchAll as $l) {
            $return .= '<p><a href="javascript:getLesson('.$l->id.');">'.substr($l->start_time,0,5).' '.$l->name.'</a></p>';
        }
        
        if(count($fetchAll)==0){
            $return .= '<p>&nbsp;</p><p>&nbsp;</p>';
        }
        elseif(count($fetchAll)==1){
            $return .= '<p>&nbsp;</p>';
        }
        return $return;
    }
    
    public function getRow($id) {
        return $this->fetchRow('id = '.$id)->toArray();
    }
    
    public function getTeacherLessons($teacher,$start,$end){
        $select = $this->getAdapter();
        $query = 'SELECT *,
                end_time-start_time as hours
                FROM lesson
                WHERE 
                teacher_id='.(int)$teacher.'
                AND start_date >= "'.$start.'"
                AND start_date <= "'.$end.'"';
        $prepare = $select->query($query);
        $result = $prepare->fetchAll(Zend_Db::FETCH_OBJ);
        $count = 0;
        $hours = 0;
        foreach($result as $row){
            $count++;
            //echo 's='.$row->start_time.' '.$row->end_time.' '.$row->hours.'<br>';
            if(!$row->end_time) {
                $hours += 1.5;
            }
            else {
               if(strlen($row->hours)==5){
                   $h = substr($row->hours,0,1);
                   $m = substr($row->hours,1,2);
                   $s = substr($row->hours,3,2);
               } else {
                   $h = substr($row->hours,0,2);
                   $m = substr($row->hours,2,2);
                   $s = substr($row->hours,4,2);
               }
               //echo $h.':'.$m.'<br>';
               $hours += ( $h + ($m/60) );
            }
            //echo $hours.'<br>';
        }
        return array('count'=>$count,'hours'=>$hours);
    }
}